package com.caih.contacts.controller;

import com.caih.contacts.entity.Employees;
import com.caih.contacts.entity.Result;
import com.caih.contacts.service.DepartmentService;
import com.caih.contacts.service.EmployessService;
import com.caih.contacts.service.WeiXinEmployeesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class EmployeesContorller {
    @Autowired
    private EmployessService employessService;
    @Autowired
    private WeiXinEmployeesService weiXinEmployeesService;


    /**
     * 保存员工
     * locahost:8080/contact/saveEmployee
     * @param employees
     * @return
     */
    @PostMapping("/saveEmployee")
    public Result<Employees> save(Employees employees){
        Result<Employees> result = weiXinEmployeesService.createE(employees,weiXinEmployeesService.getAccessToken().getToken());
        if(result.getCode() == 0) {
            employessService.save(employees);
        }else {
            result.setData(null);
        }
        return result;
    }

    /**
     *更新员工系统
     * localhost:8080/contact/updateEmployee
     * @param employees
     * @return
     */
    @PutMapping("/updateEmployee")
    public Result<Employees> update(Employees employees){
        Result<Employees> result = weiXinEmployeesService.updateE(employees,weiXinEmployeesService.getAccessToken().getToken());
        if(result.getCode() == 0){
            employessService.update(employees);
        }else {
            result.setData(null);
        }
            return result;
    }

    /**
     * 查询全部
     * @return
     */
    @GetMapping("/queryAll")
    public Result<List<Employees>> queryAll(){

        return employessService.queryAll();
    }

    /**
     * userId查询
     * localhost:8080/queryEmployee/{userid}/
     * @param id
     * @return
     */
    @GetMapping("/queryEmployee/{id}")
    public Result<Employees> queryById(@PathVariable String id){
        Result<Employees> result = employessService.queryById(id);
        if(result.getCode() != 0){
            result.setMessage("无此人");
        }
        return result;
    }


    /**
     * 通过手机号查找
     * @param mobil
     * @return
     */
    @GetMapping("/queryTel/{mobil}")
    public Result<Employees> queryByMobile(@PathVariable String mobil){
        Result<Employees> result = employessService.queryByMobile(mobil);
        if(result.getCode() != 0){
            result.setMessage("没有这个号码");
        }
        return result;
    }

    /**
     * name查询
     * @param name
     * @return
     */
    @GetMapping("/queryName/{name}")
    public Result<Employees> queryByName(@PathVariable String name){
        Result<Employees> result = employessService.queryByName(name);
        if(result.getCode() != 0){
            result.setMessage("没有这个名字的成员");
        }
        return result;

    }

    /**
     *通过id删除成员信息
     * @param id
     * @return
     */
    @GetMapping("/deleteEmployee/{id}")
    public Result<String> delete(@PathVariable String id){
        Result<String> result = weiXinEmployeesService.deleteE(id,weiXinEmployeesService.getAccessToken().getToken());
        if(result.getCode() == 0){
            employessService.deleteById(id);
        }else{
            result.setData(null);
        }
        return result;
    }
        /**
         *通过部门id（id）查询部门下的成员
         * 运行不成功
         * @param department
         * @return
         */
        @GetMapping("/queryByDept/{department}")
        public Result<List<Employees>> queryAllByDepartment(@PathVariable Integer department){
            return employessService.queryAllByDepartment(department);

        }

    /**
     *批量删除员工
     * @param useridlist
     * @return
     */
     @PostMapping("/batchdelete")
     public Result<String> batchdelete(@RequestParam(value = "useridlist",defaultValue = "") List<String> useridlist){
         Result<String> result = weiXinEmployeesService.batchDeleteE(useridlist,weiXinEmployeesService.getAccessToken().getToken());

         if(result.getCode() == 0){
             for (String test:useridlist){
                 employessService.deleteById(test);
             }
         }

         return result;
     }


}
